package com.smartcruise.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.smartcruise.R;
import com.smartcruise.util.zxing.activity.CaptureActivity;

/**
 * Created by FengChaoQun
 * on 2018/1/20 0020
 */

public class IntentUtils {
    public static String TYPE = "TYPE";
    public static String DATA = "DATA";
    public static String DATE = "DATE";
    public static String ID = "ID";

    public static final int GET_BASIC_PERMISSION = 1;

    public static final int AUDIO_RECORD = 100;
    public static final int TAKE_PICTURE = 200;
    public static final int PICK_PICTURE = 201;
    public static final int TAKE_VIDEO = 300;
    public static final int PICK_VIDEO = 301;
    public static final int SCAN_QR_CODE = 400;


    public static void takePhoto(Activity activity, Uri came_photo_path) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 调用android自带的照相机
        intent.putExtra(MediaStore.EXTRA_OUTPUT, came_photo_path);
        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
        //调用相机拍摄的时候要先确认是否有APP可以响应 防止没有可响应的APP的时候crash
        if (intent.resolveActivity(activity.getPackageManager()) != null) {
            activity.startActivityForResult(intent, TAKE_PICTURE);
        } else {
            ToastUtils.showShort(activity, activity.getString(R.string.dontSupportCamera));
        }
    }

    public static void pickPhoto(Activity activity) {
        Intent i = new Intent(Intent.ACTION_PICK, null);// 调用android的图库
        i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        activity.startActivityForResult(i, IntentUtils.PICK_PICTURE);
    }

    public static void takeVideo(Activity activity, Uri videoPath) {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, videoPath);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 3 * 30 * 1000);
        if (intent.resolveActivity(activity.getPackageManager()) != null) {
            activity.startActivityForResult(intent, TAKE_VIDEO);
        } else {
            ToastUtils.showShort(activity, activity.getString(R.string.dontSupportTakeVideo));
        }
    }

    public static void pickVideo(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        activity.startActivityForResult(intent, PICK_VIDEO);
    }

    public static void scanQR(final Activity activity) {
        Intent intent = new Intent(activity, CaptureActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivityForResult(intent, IntentUtils.SCAN_QR_CODE);
    }

    public static String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }
}
